package cn.edu.lingnan.translation.controller;


import cn.edu.lingnan.translation.entity.Menu;
import cn.edu.lingnan.translation.entity.vo.MenuParent;
import cn.edu.lingnan.translation.service.MenuService;
import cn.edu.lingnan.translation.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bao
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * @Description 获取菜单列表
     * @Param []
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 11:17
     */
    @ApiOperation(value = "获取菜单列表")
    @GetMapping("/getAllMenu")
    public R getAllMenu() {
        List<MenuParent> menu = menuService.getAllMenu();
        return R.ok().data("menus", menu);
    }

    /**
     * @Description 获取菜单列表，不分等级
     * @Param []
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/21 21:31
     */
    @ApiOperation(value = "获取菜单列表，不分等级")
    @GetMapping("/getMenuList")
    public R getMenuList() {
        List<Menu> menu = menuService.list(null);
        return R.ok().data("menus", menu);
    }

    /**
     * @Description 通过id获取菜单信息
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/15 14:17
     */
    @ApiOperation(value = "通过id获取菜单信息")
    @GetMapping("/{id}")
    public R getMenuById(@PathVariable String id) {
        Menu menu = menuService.getById(id);
        return R.ok().data("menu", menu);
    }

    /**
     * @Description 通过菜单名称获取菜单信息
     * @Param [title]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/15 14:15
     */
    @ApiOperation(value = "通过菜单名称获取菜单信息")
    @GetMapping("/title/{title}")
    public R getMenuByTitle(@PathVariable String title) {
        Page<Menu> page = new Page<>(1, 100);
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.like("title", title);
        IPage<Menu> menus = menuService.page(page, wrapper);
        return R.ok().data("menus", menus.getRecords());
    }

    /**
     * @Description 菜单添加
     * @Param [menu]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/18 2:24
     */
    @ApiOperation(value = "菜单添加")
    @PostMapping("/add")
    public R addMenu(@RequestBody Menu menu) {
        menuService.save(menu);
        return R.ok();
    }

    /**
     * @Description 菜单更新
     * @Param [menu]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/14 23:13
     */
    @ApiOperation(value = "菜单更新")
    @PostMapping("/save")
    public R saveAdminById(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return R.ok();
    }

    /**
     * @Description 菜单删除
     * @Param [id]
     * @Return cn.edu.lingnan.translation.utils.R
     * @Author BAO
     * @Date 2020/10/15 11:07
     */
    @ApiOperation(value = "菜单删除")
    @DeleteMapping("/{id}")
    public R saveAdminById(@PathVariable String id) {
        menuService.removeById(id);
        return R.ok();
    }
}

